package org.example.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution01 {
    public static void main(String[] args) {
        Solution01 sol = new Solution01();
        int[] arr = new int[]{1,2,3,4,5,6,-1};
        System.out.println(Arrays.toString(sol.RightSideView(arr)));
    }

    public int[] RightSideView (int[] nums) {
        // write code here
        int n=nums.length;
        if(n==1||n==0) return nums;
        List<Integer> list=new ArrayList();
        list.add(nums[0]);int tmpCount=1*2;int flag=0;
        while(flag+tmpCount<=n){
            int i=0;
            for(i=flag+tmpCount;i>flag;i--){
                if(nums[i]!=-1){
                    break;
                }
            }
            list.add(nums[i]);
            flag=flag+tmpCount;
            tmpCount*=2;
        }
        int[] res=new int[list.size()];
        for(int j=0;j<list.size();j++){
            res[j]=list.get(j);
        }
        return res;
    }
}
